<template>
  <el-card>
    <div class="search-input-group">
      <el-form :model="searchForm" :inline="true">
        <el-form-item style="padding:5px" label="商品编号">
          <el-input 
            v-model="searchForm.productNum" 
            placeholder="请输入商品编号" 
            clearable
          />
        </el-form-item>
        <el-form-item style="padding:5px" label="商品名称">
          <el-input 
            v-model="searchForm.productName" 
            placeholder="请输入商品名称" 
            clearable
          />
        </el-form-item>
        <el-form-item>
          <el-button type="primary" :icon="Search" @click="handleSearch">查询</el-button>
          <el-button @click="handleResetSearch">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
    
    <div class="btn-group">
      <el-button 
        v-if="btnGroup.editb" 
        type="success" 
        :icon="Edit"
        @click="showEditDialog"
        :disabled="multipleSelection.length !== 1"
      >
        修改
      </el-button>
    </div>
    
    <el-table 
      :data="dataList" 
      style="width: 100%" 
      row-key="productId"
      @selection-change="handleSelectionChange"
      border
    >
      <el-table-column type="selection" width="40" />
      <el-table-column type="index" label="序号" width="80" show-overflow-tooltip/>
      <el-table-column prop="productNum" label="商品编号" width="150" show-overflow-tooltip/>
      <el-table-column prop="productName" label="商品名称" width="150" show-overflow-tooltip/>
      <el-table-column prop="productStatus" label="当前状态" width="100" show-overflow-tooltip>
        <template #default="scope">
          <el-tag 
            :type="getProductStatusTag(scope.row.productStatus)"
            disable-transitions
          >
            {{ getStatusText(scope.row.productStatus) }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column prop="applicationId" label="申请人" width="100" show-overflow-tooltip/>
      <el-table-column prop="operateId" label="审核员编号" width="100" show-overflow-tooltip/>
      <el-table-column prop="auditDesc" label="审核描述" show-overflow-tooltip/>
    </el-table>

    <!-- 分页组件 -->
    <el-pagination
      v-model:current-page="pagination.currentPage"
      v-model:page-size="pagination.pageSize"
      :total="pagination.total"
      :page-sizes="[10, 20, 50]"
      layout="total, sizes, prev, pager, next"
      background
      @current-change="handleCurrentChange"
      class="pagination"
    />
  </el-card>
  
  <!-- 审核对话框 -->
  <el-dialog
    title="商品审核"
    v-model="dialogVisible"
    width="600px"
    @close="handleDialogClose"
  >
    <el-form
      :model="operateDataForm"
      :rules="operateDataFormRules"
      ref="operateDataFormRef"
      label-width="100px"
    >
      <el-form-item label="商品编号" prop="productNum">
        <el-input 
          v-model="operateDataForm.productNum" 
          disabled
        />
      </el-form-item>
      
      <el-form-item label="商品名称" prop="productName">
        <el-input 
          v-model="operateDataForm.productName" 
          disabled
        />
      </el-form-item>
      
      <el-form-item label="商品状态" prop="productStatus">
        <el-select v-model="operateDataForm.productStatus" placeholder="请选择商品状态">
          <el-option label="待审核" :value="0" />
          <el-option label="上架中" :value="1" />
          <el-option label="已交易" :value="2" />
          <el-option label="被封禁" :value="3" />
        </el-select>
      </el-form-item>
      
      <el-form-item label="审核描述" prop="auditDesc">
        <el-input 
          v-model="operateDataForm.auditDesc" 
          type="textarea"
          placeholder="请输入审核描述"
        />
      </el-form-item>
    </el-form>
    
    <template #footer>
      <span class="dialog-footer">
        <el-button @click="dialogVisible = false">取消</el-button>
        <el-button type="primary" @click="submitForm">确定</el-button>
      </span>
    </template>
  </el-dialog>
</template>

<script setup>
import {
    Edit,
    Search
} from '@element-plus/icons-vue'
import { onMounted, reactive, ref } from "vue"
import { getList, getSearchList, edit } from "@/api/admin/prodAudit"
import { getProductOldLevelText as getOldLevelText, getProductStatusText as getStatusText,getProductStatusTag } from "@/utils/DataTextMap"

// 模糊查询商品信息
const searchForm = reactive({
  productNum: '',
  productName: ''
})

// 对话框相关数据
const dialogVisible = ref(false)
const operateDataFormRef = ref(null)

// 审核数据表单
const operateDataForm = reactive({
  productNum: '',
  productName: '',
  productStatus: 0,
  auditDesc: ''
})

// 表单验证规则
const operateDataFormRules = {
  productStatus: [
    { required: true, message: '请选择商品状态', trigger: 'change' }
  ],
  auditDesc: [
    { required: true, message: '请输入审核描述', trigger: 'blur' }
  ]
}

// 显示编辑对话框
const showEditDialog = () => {
  dialogVisible.value = true
  // 填充表单数据
  Object.assign(operateDataForm, multipleSelection.value[0])
}

// 对话框关闭时的处理
const handleDialogClose = () => {
  operateDataFormRef.value.resetFields()
}

// 固定参数源 ---start---
const dataList = ref([])
const multipleSelection = ref([])
const isSearch = ref(false)
const btnGroup = reactive({
    editb: false
})
const pagination = reactive({
  currentPage: 1,
  pageSize: 10,
  total: 0
})

// 获取商品列表数据
const getDataList = () => {
  let res = getList(pagination.currentPage)
  res.then(res=>{
    dataList.value=res.data.records
    let opeStr=res.operateStr
    if(opeStr.includes("*")||opeStr.includes("u")) btnGroup.editb=true
    pagination.total=res.data.total
    pagination.pageSize=res.data.size
    pagination.currentPage=res.data.current
  })
}

// 搜索商品数据
const handleSearch = () => {
  isSearch.value = true
  let res=getSearchList({...searchForm},pagination.currentPage)
  res.then(res=>{ 
    dataList.value=res.data.records
    pagination.total=res.data.total
    pagination.pageSize=res.data.size
    pagination.currentPage=res.data.current
  })
}

const handleResetSearch = () => {
  searchForm.productNum = ""
  searchForm.productName = ""
  isSearch.value = false
  getDataList()
}

// 提交审核数据操作
const submitForm = () => {
  operateDataFormRef.value.validate(async (valid) => {
    if (valid) {
      // 提交审核数据
      await edit(operateDataForm)
      dialogVisible.value = false
      refreshList()
    }
  })
}

const handleSelectionChange = (selection) => {
  multipleSelection.value = selection
}

// 当前页改变时触发
const handleCurrentChange = (val) => {
  pagination.currentPage = val
  refreshList()
}

const refreshList = () => {
  if (isSearch.value) handleSearch()
  else getDataList()
}
// --- end ---

onMounted(() => {
    getDataList()
})
</script>

<style scoped>
.dialog-footer {
  display: flex;
  justify-content: flex-end;
}

.btn-group {
  margin-bottom: 15px;
}

.btn-group .el-button {
  margin-right: 1px;
}

.pagination {
  margin-top: 20px;
  display: flex;
  justify-content: center;
}

.search-input-group {
  margin-bottom: 15px;
  padding: 15px;
  background-color: #f5f7fa;
  border-radius: 4px;
}

.search-input-group .el-form-item {
  margin-bottom: 0;
  margin-right: 15px;
}
</style>